unit UnitBusquedaPaciente;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, DBGrids,
  sqldb, db, UnitMainForm, UnitNuevoInforme;

type

  { TFormBusquedaPaciente }

  TFormBusquedaPaciente = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    SQLQuery1: TSQLQuery;
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  FormBusquedaPaciente: TFormBusquedaPaciente;

implementation

{$R *.lfm}

{ TFormBusquedaPaciente }

procedure TFormBusquedaPaciente.FormShow(Sender: TObject);
var
  apellido : string;

begin
  if SQLQuery1.Active = true then
    SQLQuery1.Close;

  apellido := InputBox('Busqueda de Paciente', 'Ingrese parte del apellido del paciente', '');

  if apellido = '' then
  begin
    ShowMessage('Error en el ingreso de datos');
    close();
  end
  else
  begin
    SQLQuery1.SQL.Text := 'select * from paciente where pte_nombre like ''%' + apellido
      + '%'' order by pte_nombre';

    SQLQuery1.Open;

    DBGrid1.Columns[0].Title.Caption := 'Código';
    DBGrid1.Columns[1].Title.Caption := 'Apellido y Nombre';
    DBGrid1.Columns[2].Title.Caption := 'DNI';

    DBGrid1.Columns[0].Width := 90;
    DBGrid1.Columns[1].Width := 230;
    DBGrid1.Columns[2].Width := 110;
  end;
end;

procedure TFormBusquedaPaciente.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = true then
    SQLQuery1.Close;
end;

procedure TFormBusquedaPaciente.DBGrid1CellClick(Column: TColumn);
begin
  FormNuevoInforme.pte_cdg := SQLQuery1.FieldByName('pte_cdg').AsString;
  FormNuevoInforme.pte_dni := SQLQuery1.FieldByName('pte_dni').AsString;
  FormNuevoInforme.Edit1.Text := SQLQuery1.FieldByName('pte_nombre').AsString;

  close();
end;

end.

